<?php
namespace Home\Controller;
use Think\Controller;
class FinancialController extends CommunalController{
	//项目款项 designed by:zhangsen 2018-05-23
	function project_funds(){
		parent::check_permission('KX');
		//赋值
		if(!empty($_REQUEST['mode'])){$mode=$_REQUEST['mode'];}else{$mode=1;}
		$this->assign('mode',$mode);
		if(get_permission('KX',2)){
			if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=$this->login_center_id;}
			$this->assign('center_id',$center_id);
		}else{
			if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
			$this->assign('center_id',$center_id);
		}
		if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
		$this->assign('type',$type);
		if(!empty($_REQUEST['ptype'])){$ptype=$_REQUEST['ptype'];}else{$ptype=-10;}
		$this->assign('ptype',$ptype);
		
		if(get_permission('KX',2)){
			//所属中心下拉
			$pro_list=$this->get_db_universal_type(18,NULL,NULL,NULL,NULL,$this->login_center_id);
			$this->assign('pro_list',$pro_list);
		}else{
			//所属中心下拉
			$pro_list=$this->get_db_universal_type(18);
			$this->assign('pro_list',$pro_list);
		}
		//项目分类下拉菜单
		$ptype_list=$this->library_type_list(6,22);
		$this->assign('ptype_list',$ptype_list[0]['second']);
		if(!empty($_REQUEST['keyword']))
		{
			$map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['project_name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['_logic']='or';
			$where['_complex']=$map;
			$this->assign('keyword',$_REQUEST['keyword']);
		}
		if($center_id!=-10){$where['center_id']=$center_id;}
		if($ptype!=-10){$where['p_type']=$ptype;}
		$db_project=M('db_project');
		$db_project_funds=M('db_project_funds');
		$db_project_funds_stage=M('db_project_funds_stage');
		$db_project_funds_ticket=M('db_project_funds_ticket');
		$db_project_funds_money=M('db_project_funds_money');
		$pro=$db_project->where($where)->field('db_project.*')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->select();
		foreach($pro as $ka=>$va){
			if($ka==0){
				$projects=$va['id'];
			}else{
				$projects.=','.$va['id'];
			}
		}
		if($mode==1){
			if($type==-10){
				$where_s['db_project_funds.pid']=array('in',$projects);
				$fp=$db_project_funds_ticket->field('db_project_funds_ticket.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds.id as fid')->where($where_s)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('id desc')->select();
				$fk=$db_project_funds_money->field('db_project_funds_money.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds_ticket.id as kid,db_project_funds_ticket.money as fmoney,db_project_funds_ticket.date as fdate,db_project_funds.id as fid,invoice')->where($where_s)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('id desc')->select();
				$detail = array_merge((array)$fp,(array)$fk);
				$result=$this->multi_array_sort($detail,'add_date',SORT_DESC);
				foreach($result as $key=>$value){
					unset($stag);
					$result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
					$result[$key]['add_user']=$this->get_username($value['add_uid']);
					if(!empty($value['tid'])){
						//开票
						$where_kp['tid']=$value['tid'];
						$where_kp['add_date']=array('elt',$value['add_date']);
						$ykfp=$db_project_funds_ticket->where($where_kp)->sum('money');
						if(($value['ticket_money']-$ykfp)>=0){$result[$key]['syykp']=$value['ticket_money']-$ykfp;}else{$result[$key]['syykp']=0;}
						$result[$key]['fmoney']=$value['money'];
						$result[$key]['fdate']=$value['date'];
						//能否删除
						$where_del['id']=$value['tid'];
						$max_stage=$db_project_funds_stage->where($where_del)->find();
						$where_da['mid']=$max_stage['mid'];
						$stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
						foreach($stages as $kk=>$vv){
							if($kk==0){
								$stag=$vv['id'];
							}else{
								$stag.=','.$vv['id'];
							}
						}
						$where_dele['tid']=array('in',$stag);
						$dele=$db_project_funds_ticket->where($where_dele)->order('id desc')->find();
						if($dele['id']==$value['id']){
							$where_h['sid']=$value['id'];
							$has=$db_project_funds_money->where($where_h)->find();
							if(empty($has)){$result[$key]['del']=1;}
						}
						//能否编辑
						$where_e['sid']=$value['id'];
						$edit=$db_project_funds_money->where($where_e)->find();
						if(empty($edit)){$result[$key]['edit']=1;}
					}else{
						//收款
						$where_sk['sid']=$value['sid'];
						$where_sk['add_date']=array('elt',$value['add_date']);
						$ysk=$db_project_funds_money->where($where_sk)->sum('money');
						if(($value['fmoney']-$ysk)>=0){$result[$key]['syysk']=$value['fmoney']-$ysk;}else{$result[$key]['syysk']=0;}
						//能否删除
						$where_d['id']=$value['sid'];
						$stage=$db_project_funds_ticket->where($where_d)->find();
						$where_del['id']=$stage['tid'];
						$max_stage=$db_project_funds_stage->where($where_del)->find();
						$where_da['mid']=$max_stage['mid'];
						$stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
						foreach($stages as $kk=>$vv){
							if($kk==0){
								$stag=$vv['id'];
							}else{
								$stag.=','.$vv['id'];
							}
						}
						$where_dele['tid']=array('in',$stag);
						$dele=$db_project_funds_ticket->field('id')->where($where_dele)->select();
						foreach($dele as $k3=>$v3){
							if($k3==0){
								$sta=$v3['id'];
							}else{
								$sta.=','.$v3['id'];
							}
						}
						$where_maxd['sid']=array('in',$sta);
						$maxdele=$db_project_funds_money->where($where_maxd)->order('id desc')->find();
						if($maxdele['id']==$value['id']){$result[$key]['del']=1;}
					}
				}
				$count=count($result);
				$Page = new \Think\Page($count,15);
				$result=array_slice($result, $Page->firstRow,$Page->listRows);
				$show=$Page->show();
			}elseif($type==1){
				$where_s['db_project_funds.pid']=array('in',$projects);
				$count=$db_project_funds_ticket->field('db_project_funds_ticket.id')->where($where_s)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('db_project_funds_ticket.id desc')->count();
				$Page = new \Think\Page($count,C('PAGE_LIST_ROWS'));
				$show=$Page->show();
				$result=$db_project_funds_ticket->field('db_project_funds_ticket.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds.id as fid')->where($where_s)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->limit($Page->firstRow.','.$Page->listRows)->order('db_project_funds_ticket.id desc')->select();
				foreach($result as $key=>$value){
					unset($stag);
					$result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
					$result[$key]['add_user']=$this->get_username($value['add_uid']);
					//开票
					$where_kp['tid']=$value['tid'];
					$where_kp['add_date']=array('elt',$value['add_date']);
					$ykfp=$db_project_funds_ticket->where($where_kp)->sum('money');
					if(($value['ticket_money']-$ykfp)>=0){$result[$key]['syykp']=$value['ticket_money']-$ykfp;}else{$result[$key]['syykp']=0;}
					$result[$key]['fmoney']=$value['money'];
					$result[$key]['fdate']=$value['date'];
					//能否删除
						$where_del['id']=$value['tid'];
						$max_stage=$db_project_funds_stage->where($where_del)->find();
						$where_da['mid']=$max_stage['mid'];
						$stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
						foreach($stages as $kk=>$vv){
							if($kk==0){
								$stag=$vv['id'];
							}else{
								$stag.=','.$vv['id'];
							}
						}
						$where_dele['tid']=array('in',$stag);
						$dele=$db_project_funds_ticket->where($where_dele)->order('id desc')->find();
						if($dele['id']==$value['id']){
							$where_h['sid']=$value['id'];
							$has=$db_project_funds_money->where($where_h)->find();
							if(empty($has)){$result[$key]['del']=1;}
						}
						//能否编辑
						$where_e['sid']=$value['id'];
						$edit=$db_project_funds_money->where($where_e)->find();
						if(empty($edit)){$result[$key]['edit']=1;}
				}
			}elseif($type==2){
				$where_s['db_project_funds.pid']=array('in',$projects);
				$count=$db_project_funds_money->field('db_project_funds_money.id')->where($where_s)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->count();
				$Page = new \Think\Page($count,C('PAGE_LIST_ROWS'));
				$show=$Page->show();
				$result=$db_project_funds_money->field('db_project_funds_money.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds_ticket.money as fmoney,db_project_funds_ticket.id as kid,db_project_funds_ticket.date as fdate,db_project_funds.id as fid,invoice')->where($where_s)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->limit($Page->firstRow.','.$Page->listRows)->order('db_project_funds_money.id desc')->select();
				foreach($result as $key=>$value){
					unset($stag);
					$result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
					$result[$key]['add_user']=$this->get_username($value['add_uid']);
					//收款
					$where_sk['sid']=$value['sid'];
					$where_sk['add_date']=array('elt',$value['add_date']);
					$ysk=$db_project_funds_money->where($where_sk)->sum('money');
					if(($value['fmoney']-$ysk)>=0){$result[$key]['syysk']=$value['fmoney']-$ysk;}else{$result[$key]['syysk']=0;}
					//能否删除
						$where_d['id']=$value['sid'];
						$stage=$db_project_funds_ticket->where($where_d)->find();
						$where_del['id']=$stage['tid'];
						$max_stage=$db_project_funds_stage->where($where_del)->find();
						$where_da['mid']=$max_stage['mid'];
						$stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
						foreach($stages as $kk=>$vv){
							if($kk==0){
								$stag=$vv['id'];
							}else{
								$stag.=','.$vv['id'];
							}
						}
						$where_dele['tid']=array('in',$stag);
						$dele=$db_project_funds_ticket->field('id')->where($where_dele)->select();
						foreach($dele as $k3=>$v3){
							if($k3==0){
								$sta=$v3['id'];
							}else{
								$sta.=','.$v3['id'];
							}
						}
						$where_maxd['sid']=array('in',$sta);
						$maxdele=$db_project_funds_money->where($where_maxd)->order('id desc')->find();
						if($maxdele['id']==$value['id']){$result[$key]['del']=1;}
				}
			}
		}else{
			$where_s['db_project_funds.pid']=array('in',$projects);
			$count=$db_project_funds->field('db_project_funds.id')->where($where_s)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->count();
			$Page = new \Think\Page($count,C('PAGE_LIST_ROWS'));
			$show=$Page->show();
			$result=$db_project_funds->field('db_project_funds.*,project_num,project_name,p_type')->where($where_s)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->limit($Page->firstRow.','.$Page->listRows)->order('db_project_funds.id desc')->select();
			foreach($result as $key=>$value){
				$max=0;
				$fp=0;
				$sk=0;
				$result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
				$result[$key]['add_user']=$this->get_username($value['add_uid']);
				$where_t['mid']=$value['id'];
				$result[$key]['two']=$db_project_funds_stage->where($where_t)->order('stage')->select();
				foreach($result[$key]['two'] as $k2=>$v2){
					$where_th['tid']=$v2['id'];
					$result[$key]['two'][$k2]['three']=$db_project_funds_ticket->where($where_th)->order('id desc')->select();
					if(!empty($result[$key]['two'][$k2]['three'])){$result[$key]['ndel']=1;}
					$fp=count($result[$key]['two'][$k2]['three']);
					foreach($result[$key]['two'][$k2]['three'] as $k3=>$v3){
						$where_kp['tid']=$v3['tid'];
						$where_kp['add_date']=array('elt',$v3['add_date']);
						$ykfp=$db_project_funds_ticket->where($where_kp)->sum('money');
						if($v2['ticket_money']-$ykfp>=0){$result[$key]['two'][$k2]['three'][$k3]['syykp']=$v2['ticket_money']-$ykfp;}else{$result[$key]['two'][$k2]['three'][$k3]['syykp']=0;}
						if($v3['tid']==$v2['id']){if($v2['stage']==count($result[$key]['two'])){$result[$key]['nedit']=1;}}
						$where_f['sid']=$v3['id'];
						$result[$key]['two'][$k2]['three'][$k3]['four']=$db_project_funds_money->where($where_f)->order('id desc')->select();
						foreach($result[$key]['two'][$k2]['three'][$k3]['four'] as $k4=>$v4){
							$where_sk['sid']=$v4['sid'];
							$where_sk['add_date']=array('elt',$v4['add_date']);
							$ysk=$db_project_funds_money->where($where_sk)->sum('money');
							if($v3['money']-$ysk>=0){$result[$key]['two'][$k2]['three'][$k3]['four'][$k4]['syysk']=$v3['money']-$ysk;}else{$result[$key]['two'][$k2]['three'][$k3]['four'][$k4]['syysk']=0;}
						}
						$sk=count($result[$key]['two'][$k2]['three'][$k3]['four']);
						$max=max($fp,$sk);
						if(empty($result[$key]['two'][$k2]['three'][$k3]['four'])){$result[$key]['two'][$k2]['three'][$k3]['max3']=1;}else{$result[$key]['two'][$k2]['three'][$k3]['max3']=$sk;}
						if($type==1){$result[$key]['two'][$k2]['three'][$k3]['max3']=1;}
						$result[$key]['two'][$k2]['max2']+=$result[$key]['two'][$k2]['three'][$k3]['max3'];
					}
					if(empty($result[$key]['two'][$k2]['three'])){$result[$key]['two'][$k2]['max2']=1;}
					$result[$key]['max']+=$result[$key]['two'][$k2]['max2'];
				}
			}
		}
		$this->assign('result',$result);
		$this->assign('page',$show);
		$this->display();
	}
	
	//项目款项-->弹框详情/删除 designed by:zhangsen 2018-05-23
	function project_funds_detail(){
		$db_project_funds=M('db_project_funds');
		$db_project_funds_stage=M('db_project_funds_stage');
		$db_project_funds_ticket=M('db_project_funds_ticket');
		$db_project_funds_money=M('db_project_funds_money');
		if($_REQUEST['action']=='one'){
			$where['db_project_funds.id']=$_REQUEST['id'];
			if($_REQUEST['operation']=='del'){
				$where_d['id']=$_REQUEST['id'];
				$result_d=$db_project_funds->where($where_d)->find();
				//删除附件
				$url=$result_d['file_url'];
				$file=str_replace("/dms_erp/uploads", "./uploads",$url);
				if(file_exists($file)) {
					@unlink($file);
				}
				$where_f['mid']=$_REQUEST['id'];
				$db_project_funds_stage->where($where_f)->delete();
				$db_project_funds->where($where)->delete();
				$data['status']=self::YES;
				$data['msg']='操作成功';
			}else{
				$data['main']=$db_project_funds->field('db_project_funds.*,project_num,project_name,center_id')->where($where)->join('left join db_project on db_project_funds.pid=db_project.id')->find();
				$type_list=$this->get_db_universal_type(3,1,NULL,NULL,NULL,$data['main']['center_id']);
				$data['main']['center_name']=$type_list['name1'];
				$data['main']['contract_money']=$data['main']['contract_money']?$data['main']['contract_money']+0:'';
				$data['main']['add_user']=$this->get_username($data['main']['add_uid']);
				$where_f['mid']=$_REQUEST['id'];
				$data['stage']=$db_project_funds_stage->where($where_f)->select();
				foreach($data['stage'] as $kk=>$vv){
					$data['stage'][$kk]['bili']=$vv['bili']?($vv['bili']+0).'%':'';
					$data['stage'][$kk]['stage_money']=$vv['stage_money']+0;
					$data['stage'][$kk]['ticket_money']=$vv['ticket_money']+0;
				}
			}
		}elseif($_REQUEST['action']=='two'){
			$where['db_project_funds_ticket.id']=$_REQUEST['id'];
			if($_REQUEST['operation']=='del'){
				$db_project_funds_ticket->where($where)->delete();
				$data['status']=self::YES;
				$data['msg']='操作成功';
			}else{
				$data=$db_project_funds_ticket->field('db_project_funds_ticket.*,stage,project_num,project_name,ticket_money,ticket_date,db_project_funds.id as fid,contract_money,contract_date,db_project_funds.file_name,db_project_funds.file_url')->where($where)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->find();
				//已开票金额
				$where_s['tid']=$data['tid'];
				$where_s['id']=array('neq',$data['id']);
				$ykp=$db_project_funds_ticket->where($where_s)->sum('money');
				if($ykp){$data['ykp']=$ykp;}else{$data['ykp']=0;}
				$data['contract_money']=$data['contract_money']?$data['contract_money']+0:'';
				$data['ticket_money']=$data['ticket_money']+0;
				$data['money']=$data['money']+0;
				$data['add_user']=$this->get_username($data['add_uid']);
			}
		}elseif($_REQUEST['action']=='three'){
			$where['db_project_funds_money.id']=$_REQUEST['id'];
			if($_REQUEST['operation']=='del'){
				$db_project_funds_money->where($where)->delete();
				$data['status']=self::YES;
				$data['msg']='操作成功';
			}else{
				$data=$db_project_funds_money->field('db_project_funds_money.*,stage,project_num,project_name,ticket_money,ticket_date,db_project_funds_ticket.money as fmoney,db_project_funds_ticket.date as fdate,db_project_funds.id as fid,invoice,contract_money,contract_date,db_project_funds.file_name,db_project_funds.file_url')->where($where)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->find();
				//发票已收款金额
				$where_s['sid']=$data['sid'];
				$where_s['id']=array('neq',$data['id']);
				$ysk=$db_project_funds_money->where($where_s)->sum('money');
				if($ysk){$data['ysk']=$ysk;}else{$data['ysk']=0;}
				$data['contract_money']=$data['contract_money']?$data['contract_money']+0:'';
				$data['fmoney']=$data['fmoney']+0;
				$data['money']=$data['money']+0;
				$data['add_user']=$this->get_username($data['add_uid']);
			}
		}
		echo json_encode($data);die;
	}
	
	//开票&收款汇总表 designed by:zhangsen 2018-05-23
	function project_funds_total(){
		parent::check_permission('KS');
		//赋值
		$date=new \DateTime();
		if(get_permission('KS',2)){
			if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=$this->login_center_id;}
			$this->assign('center_id',$center_id);
		}else{
			if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
			$this->assign('center_id',$center_id);
		}
		if(!empty($_REQUEST['ptype'])){$ptype=$_REQUEST['ptype'];}else{$ptype=-10;}
		$this->assign('ptype',$ptype);
		$date->modify('this week');
		if(!empty($_REQUEST['start'])){$start=$_REQUEST['start'];}else{$start=$date->format('Y-m-d');}
		$this->assign('start',$start);
		$date->modify('this week +6 days');
		if(!empty($_REQUEST['end'])){$end=$_REQUEST['end'];}else{$end=$date->format('Y-m-d');}
		$this->assign('end',$end);
		if(!empty($_REQUEST['kp_type'])){$kp_type=$_REQUEST['kp_type'];}else{$kp_type=-10;}
		$this->assign('kp_type',$kp_type);
		if(!empty($_REQUEST['sk_type'])){$sk_type=$_REQUEST['sk_type'];}else{$sk_type=-10;}
		$this->assign('sk_type',$sk_type);
		//上一周期，下一周期
		$this->assign('last_url',__APP__."/Financial/project_funds_total/keyword/".$_REQUEST['keyword']."/center_id/".$center_id."/ptype/".$ptype."/start/".$this->start_end_cycle_adjust(1,$start,1,4)."/end/".$this->start_end_cycle_adjust(2,$end,1,4)."/kp_type/".$kp_type."/sk_type/".$sk_type);
		$this->assign('next_url',__APP__."/Financial/project_funds_total/keyword/".$_REQUEST['keyword']."/center_id/".$center_id."/ptype/".$ptype."/start/".$this->start_end_cycle_adjust(1,$start,2,4)."/end/".$this->start_end_cycle_adjust(2,$end,2,4)."/kp_type/".$kp_type."/sk_type/".$sk_type);
		//所属中心下拉
		if(get_permission('KS',2)){
			//所属中心下拉
			$pro_list=$this->get_db_universal_type(18,NULL,NULL,NULL,NULL,$this->login_center_id);
		}else{
			//所属中心下拉
			$pro_list=$this->get_db_universal_type(18);
		}
		$this->assign('pro_list',$pro_list);
		//项目分类下拉菜单
		$ptype_list=$this->library_type_list(6,22);
		$this->assign('ptype_list',$ptype_list[0]['second']);
		if(!empty($_REQUEST['keyword']))
		{
			$map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['project_name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['_logic']='or';
			$where['_complex']=$map;
			$this->assign('keyword',$_REQUEST['keyword']);
		}
		if($center_id!=-10){$where['center_id']=$center_id;}
		if($ptype!=-10){$where['p_type']=$ptype;}
		$db_project=M('db_project');
		$db_project_funds=M('db_project_funds');
		$db_project_funds_stage=M('db_project_funds_stage');
		$db_project_funds_ticket=M('db_project_funds_ticket');
		$db_project_funds_money=M('db_project_funds_money');
		$pro=$db_project->where($where)->field('db_project.*')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->select();
		foreach($pro as $ka=>$va){
			if($ka==0){
				$projects=$va['id'];
			}else{
				$projects.=','.$va['id'];
			}
		}
		if(!empty($projects)){
			$where_s['db_project_funds.pid']=array('in',$projects);
		}else{
			$where_s['db_project_funds.pid']=-1;
		}
		$where_s['db_project_funds.add_date']=array('between',array($start,$end));
		$result=$db_project_funds->field('db_project_funds.*,project_num,project_name,p_type')->where($where_s)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('db_project_funds.id desc')->select();
		foreach($result as $key=>$value){
			unset($where_k);
			$result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
			//发票
			$where_k['mid']=$value['id'];
			$result[$key]['yikp']=$db_project_funds_stage->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_k)->sum('money')+0;
			$max_stage=$db_project_funds_stage->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_k)->order('stage desc')->find();
			if(empty($max_stage)){$where_k['stage']=array('elt',1);}else{$where_k['stage']=array('elt',$max_stage['stage']);}
			$result[$key]['yinkp']=$db_project_funds_stage->where($where_k)->sum('ticket_money')+0;
			$result[$key]['syykp']=$result[$key]['yinkp']-$result[$key]['yikp'];
			//收款
			$result[$key]['yisk']=$db_project_funds_money->field('db_project_funds_ticket.id')->join('inner join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('inner join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->where($where_k)->order('db_project_funds_ticket.id desc')->sum('db_project_funds_money.money')+0;
			if(empty($result[$key]['yisk'])){$result[$key]['yisk_lj']=-1;}
			$result[$key]['syysk']=$result[$key]['yikp']-$result[$key]['yisk'];
			$where_cs['mid']=$value['id'];
			$count_stage=$db_project_funds_stage->where($where_cs)->count();
			if($count_stage==intval($max_stage['stage'])){
				$result[$key]['title']='当前阶段已是最后一个阶段';
			}else{
				$where_n['mid']=$value['id'];
				if(empty($max_stage)){
					$where_n['stage']=(intval($max_stage['stage'])+2);
				}else{
					$where_n['stage']=(intval($max_stage['stage'])+1);
				}
				$next=$db_project_funds_stage->where($where_n)->find();
				$result[$key]['title']='下一阶段应开票金额(日期)：'.($next['ticket_money']+0).'('.$next['ticket_date'].')';
				
			}
			if(empty($max_stage)){
				$where_on['mid']=$value['id'];
				$where_on['stage']=1;
				$so=$db_project_funds_stage->where($where_on)->find();
				$result[$key]['ticket_date']=$so['ticket_date'];
				$result[$key]['warn_date']=$so['warn_date'];
				$result[$key]['press_date']=$so['press_date'];
				if(strtotime(date("Y-m-d"))>strtotime($so['press_date']) and $result[$key]['yinkp']>$result[$key]['yikp']){$result[$key]['yikp_lj']=-1;}
			}else{
				$result[$key]['ticket_date']=$max_stage['ticket_date'];
				$result[$key]['warn_date']=$max_stage['warn_date'];
				$result[$key]['press_date']=$max_stage['press_date'];
				if(strtotime(date("Y-m-d"))>strtotime($max_stage['press_date']) and $result[$key]['yinkp']>$result[$key]['yikp']){$result[$key]['yikp_lj']=-1;}
			}
			//筛选
			if($kp_type==1){
				if($result[$key]['syykp']>0){
					unset($result[$key]);
				}
			}elseif($kp_type==-1){
				if($result[$key]['syykp']<=0){
					unset($result[$key]);
				}
			}
			if($sk_type==1){
				if($result[$key]['syysk']>0){
					unset($result[$key]);
				}
			}elseif($sk_type==-1){
				if($result[$key]['syysk']<=0){
					unset($result[$key]);
				}
			}
		}
		//合计
		foreach($result as $kk=>$vv){
			$sum['contract_money']+=$vv['contract_money'];
			$sum['yinkp']+=$vv['yinkp'];
			$sum['yikp']+=$vv['yikp'];
			$sum['syykp']+=$vv['syykp'];
			$sum['yisk']+=$vv['yisk'];
			$sum['syysk']+=$vv['syysk'];
		}
		$count=count($result);
		$Page = new \Think\Page($count,999);
		$result=array_slice($result, $Page->firstRow,$Page->listRows);
		$show=$Page->show();
		$this->assign('result',$result);
		$this->assign('page',$show);
		$this->assign('sum',$sum);
		$this->assign('result',$result);
		$this->display();
	}

	//开票&收款汇总表-->弹框详情 designed by:zhangsen 2018-05-23
	function project_funds_total_ajax(){
		$db_project_funds=M('db_project_funds');
		$db_project_funds_stage=M('db_project_funds_stage');
		$db_project_funds_ticket=M('db_project_funds_ticket');
		$db_project_funds_money=M('db_project_funds_money');
		if($_REQUEST['action']=='one'){
			$where['db_project_funds.id']=$_REQUEST['id'];
			$data['main']=$db_project_funds->field('db_project_funds.*,project_num,project_name,center_id')->where($where)->join('left join db_project on db_project_funds.pid=db_project.id')->find();
			$type_list=$this->get_db_universal_type(3,1,NULL,NULL,NULL,$data['main']['center_id']);
			$data['main']['center_name']=$type_list['name1'];
			$data['main']['contract_money']=$data['main']['contract_money']?$data['main']['contract_money']+0:'';
			$data['main']['add_user']=$this->get_username($data['main']['add_uid']);
			$where_f['mid']=$_REQUEST['id'];
			$data['stage']=$db_project_funds_stage->where($where_f)->select();
			foreach($data['stage'] as $kk=>$vv){
				$data['stage'][$kk]['bili']=$vv['bili']?($vv['bili']+0).'%':'';
				$data['stage'][$kk]['stage_money']=$vv['stage_money']+0;
				$data['stage'][$kk]['ticket_money']=$vv['ticket_money']+0;
			}
		}elseif($_REQUEST['action']=='two'){
			$where['db_project_funds.id']=$_REQUEST['id'];
			$data=$db_project_funds_ticket->field('db_project_funds_ticket.*,stage,project_num,project_name,ticket_money,ticket_date,db_project_funds.id as fid,contract_money,contract_date')->where($where)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->select();
			foreach($data as $key=>$value){
				//已开票金额
				$where_s['tid']=$value['tid'];
				$where_s['id']=array('neq',$value['id']);
				$ykp=$db_project_funds_ticket->where($where_s)->sum('money');
				if($ykp){$data[$key]['ykp']=$ykp;}else{$data[$key]['ykp']=0;}
				$data[$key]['contract_money']=$value['contract_money']?$value['contract_money']+0:NULL;
				$data[$key]['ticket_money']=$value['ticket_money']+0;
				$data[$key]['money']=$value['money']+0;
			}
		}elseif($_REQUEST['action']=='three'){
			$where['db_project_funds.id']=$_REQUEST['id'];
			$data=$db_project_funds->field('db_project_funds.*,project_num,project_name,p_type')->where($where)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->find();
			$data['p_typen']=$this->get_db_universal_label(2,$data['p_type']);
			$data['add_user']=$this->get_username($data['add_uid']);
			$data['contract_money']=$data['contract_money']?$data['contract_money']+0:'';
			$where_t['mid']=$data['id'];
			$data['two']=$db_project_funds_stage->where($where_t)->order('stage')->select();
			foreach($data['two'] as $k2=>$v2){
				$where_th['tid']=$v2['id'];
				$data['two'][$k2]['three']=$db_project_funds_ticket->where($where_th)->order('id desc')->select();
				$data['two'][$k2]['sum']=$db_project_funds_ticket->where($where_th)->order('id desc')->sum('money')+0;
				$fp=count($data['two'][$k2]['three']);
				foreach($data['two'][$k2]['three'] as $k3=>$v3){
					$data['two'][$k2]['three'][$k3]['add_user']=$this->get_username($v3['add_uid']);
					$where_kp['tid']=$v3['tid'];
					$where_kp['add_date']=array('elt',$v3['add_date']);
					$ykfp=$db_project_funds_ticket->where($where_kp)->sum('money');
					if($v2['ticket_money']-$ykfp>=0){$data['two'][$k2]['three'][$k3]['syykp']=$v2['ticket_money']-$ykfp;}else{$data['two'][$k2]['three'][$k3]['syykp']=0;}
					if($v3['tid']==$v2['id']){if($v2['stage']==count($data['two'])){$data['nedit']=1;}}
					$where_f['sid']=$v3['id'];
					
					//发票已收款金额
					$where_s['sid']=$v3['id'];
					$ysk=$db_project_funds_money->where($where_s)->sum('money');
					if($ysk){$data['two'][$k2]['three'][$k3]['ysk']=$ysk;}else{$data['two'][$k2]['three'][$k3]['ysk']=0;}
					if($_REQUEST['type']==3){
						$data['two'][$k2]['three'][$k3]['four']=$db_project_funds_money->where($where_f)->order('id desc')->select();
						foreach($data['two'][$k2]['three'][$k3]['four'] as $k4=>$v4){
							$data['two'][$k2]['three'][$k3]['four'][$k4]['add_user']=$this->get_username($v4['add_uid']);
							$where_sk['sid']=$v4['sid'];
							$where_sk['add_date']=array('elt',$v4['add_date']);
							$ysk=$db_project_funds_money->where($where_sk)->sum('money');
							if($v3['money']-$ysk>=0){$data['two'][$k2]['three'][$k3]['four'][$k4]['syysk']=$v3['money']-$ysk;}else{$data['two'][$k2]['three'][$k3]['four'][$k4]['syysk']=0;}
						}
					}
					
					$sk=count($data['two'][$k2]['three'][$k3]['four']);
					$max=max($fp,$sk);
					if(empty($data['two'][$k2]['three'][$k3]['four'])){$data['two'][$k2]['three'][$k3]['max3']=1;}else{$data['two'][$k2]['three'][$k3]['max3']=$sk;}
					if($type==1){$data['two'][$k2]['three'][$k3]['max3']=1;}
					$data['two'][$k2]['max2']+=$data['two'][$k2]['three'][$k3]['max3'];
				}
				if(empty($data['two'][$k2]['three'])){$data['two'][$k2]['max2']=1;}
				$data['max']+=$data['two'][$k2]['max2'];
			}
			/*
			$where['db_project_funds.id']=$_REQUEST['id'];
			$data=$db_project_funds_money->field('db_project_funds_money.*,stage,project_num,project_name,ticket_money,ticket_date,db_project_funds_ticket.money as fmoney,db_project_funds_ticket.date as fdate,db_project_funds.id as fid,invoice,contract_money,contract_date')->where($where)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->select();
			foreach($data as $key=>$value){
				//发票已收款金额
				$where_s['sid']=$value['sid'];
				$where_s['id']=array('neq',$value['id']);
				$ysk=$db_project_funds_money->where($where_s)->sum('money');
				if($ysk){$data[$key]['ysk']=$ysk;}else{$data[$key]['ysk']=0;}
				$data[$key]['contract_money']=$value['contract_money']+0;
				$data[$key]['fmoney']=$value['fmoney']+0;
				$data[$key]['money']=$value['money']+0;
			}*/echo json_encode($data);die;
		}
		echo json_encode($data);die;
	}
	
	//项目款项-->添加 designed by:zhangsen 2018-05-23
	function project_funds_add(){
		$pro_list=$this->get_db_universal_type(18);
		$this->assign('pro_list',$pro_list);
		$this->assign('mode',$_REQUEST['mode']);
		if($_REQUEST['action']=='add'){
			//添加主单
			$db_project_funds=M('db_project_funds');
			$where['pid']=$_REQUEST['pid'];
			if(!empty($_REQUEST['contract_money'])){$where['contract_money']=$_REQUEST['contract_money'];}
			$where['contract_date']=$_REQUEST['contract_date'];
			if(!empty($_REQUEST['file_name'])){$where['file_name']=$_REQUEST['file_name'];}
			if(!empty($_REQUEST['file_url'])){$where['file_url']=$_REQUEST['file_url'];}
			$where['stages']=$_REQUEST['stages'];
			$where['add_uid']=$this->login_id;
			$where['add_date']=date('Y-m-d H:i:s');
			$result_bool=$db_project_funds->add($where);
			if($result_bool){
				//添加副单
				$db_project_funds_stage=M('db_project_funds_stage');
				foreach($_REQUEST['stage_money'] as $key=>$value){
					if($value){
						$where_d['mid']=$result_bool;
						$where_d['stage']=$_REQUEST['stage'][$key];
						if($result_bool['contract_money']){
							$where_d['bili']=$_REQUEST['bili'][$key];
						}else{
							$where_d['bili']=NULL;
						}
						$where_d['stage_money']=$_REQUEST['stage_money'][$key];
						$where_d['stage_date']=$_REQUEST['stage_date'][$key];
						$where_d['ticket_money']=$_REQUEST['ticket_money'][$key];
						$where_d['ticket_date']=$_REQUEST['ticket_date'][$key];
						$where_d['press_date']=$_REQUEST['press_date'][$key];
						$where_d['warn_date']=$_REQUEST['warn_date'][$key];
						$db_project_funds_stage->add($where_d);
					}
				}
			}
			$this->success('操作成功',__APP__."/Financial/project_funds/mode/".$_REQUEST['mode']);die;
		}
		$this->display();
	}
	
	//项目款项-->编辑 designed by:zhangsen 2018-05-23
	function project_funds_edit(){
		$db_project_funds=M('db_project_funds');
		$where_w['db_project_funds.id']=$_REQUEST['id'];
		$result=$db_project_funds->field('db_project_funds.*,center_id,project_num,project_name')->join('left join db_project on db_project_funds.pid=db_project.id')->where($where_w)->find();
		$center_name=$this->get_db_universal_type(18,1,NULL,NULL,NULL,$result['center_id']);
		$result['center_name']=$center_name['name1'];
		//排除已经添加的项目
		$where_p['id']=array('neq',$_REQUEST['id']);
		$result_funds=$db_project_funds->field('pid')->where($where_p)->group('pid')->select();
		if($result_funds){
			foreach($result_funds as $key=>$value){
				$pids.=$value['pid'].',';
			}
			$where['db_project.id']=array('not in',rtrim($pids,','));
		}
		$db_project=M('db_project');
		$where_r['center_id']=$result['center_id'];
		$where_r['db_project_bg.status']=1;
		$result_p=$db_project->field('db_project.*,db_project_bg.status')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->where($where_r)->order('db_project_bg.pid desc')->select();
		//阶段详情
		$db_project_funds_stage=M('db_project_funds_stage');
		$where_d['mid']=$_REQUEST['id'];
		$detail=$db_project_funds_stage->field('db_project_funds_stage.*')->where($where_d)->order('stage')->select();
		$sum=NULL;
		foreach($detail as $key=>$value){
			$sum+=$value['stage_money'];
		}
		$result['sum']=$sum;
		$this->assign('result',$result);
		$this->assign('result_p',$result_p);
		$this->assign('detail',$detail);
		$pro_list=$this->get_db_universal_type(18);
		$this->assign('pro_list',$pro_list);
		//是否可以编辑
		$where_i['mid']=$_REQUEST['id'];
		$result_t=$db_project_funds_stage->field('max(stage) as stage')->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_i)->find();
		$this->assign('is_edit',$result_t['stage']);		
		if($_REQUEST['action']=='edit'){
			$db_project_funds_stage=M('db_project_funds_stage');
			if($_REQUEST['is_edit']){
				foreach($_REQUEST['stage_money'] as $key=>$value){
					if($value){
						$where_d['id']=$_REQUEST['did'][$key];
						$where_d['mid']=$_REQUEST['id'];
						$where_d['stage']=$_REQUEST['stage'][$key];
						if($result['contract_money']){
							$where_d['bili']=$_REQUEST['bili'][$key]?$_REQUEST['bili'][$key]:0;
						}else{
							$where_d['bili']=NULL;
						}
						$where_d['stage_money']=$_REQUEST['stage_money'][$key];
						$where_d['stage_date']=$_REQUEST['stage_date'][$key];
						$where_d['ticket_money']=$_REQUEST['ticket_money'][$key];
						$where_d['ticket_date']=$_REQUEST['ticket_date'][$key];
						$where_d['press_date']=$_REQUEST['press_date'][$key];
						$where_d['warn_date']=$_REQUEST['warn_date'][$key];
						$db_project_funds_stage->save($where_d);
					}
				}
			}else{
				//编辑主单
				if(!empty($_REQUEST['file_name'])){
					$where['file_name']=$_REQUEST['file_name'];
					if($_REQUEST['file_url']){
						//判断是否修改过文件
						if($_REQUEST['file_url']!=$result['file_url']){
							$where['file_url']=$_REQUEST['file_url'];
							//删除原文件
							$file1=str_replace("/dms_erp/uploads", "./uploads",$result['file_url']);
							if (file_exists($file1)){
								@unlink($file1);
							}
						}
					}else{
						$where['file_url']=NULL;
						if($result['file_url']){
							//删除原文件
							$file1=str_replace("/dms_erp/uploads", "./uploads",$result['file_url']);
							if(file_exists($file1)){
								@unlink($file1);
							}
						}
					}
				}else{
					$where['file_name']=NULL;
					$where['file_url']=NULL;
					if($result['file_url']){
						//删除原文件
						$file1=str_replace("/dms_erp/uploads", "./uploads",$result['file_url']);
						if(file_exists($file1)){
							@unlink($file1);
						}
					}
				}
				$where['id']=$_REQUEST['id'];
				$where['pid']=$_REQUEST['pid'];
				if(!empty($_REQUEST['contract_money'])){$where['contract_money']=$_REQUEST['contract_money'];}else{$where['contract_money']=NULL;}
				$where['contract_date']=$_REQUEST['contract_date'];
				$where['stages']=$_REQUEST['stages'];
				$result_bool=$db_project_funds->save($where);
				$where_s['mid']=$_REQUEST['id'];
				$db_project_funds_stage->where($where_s)->delete();
				foreach($_REQUEST['stage_money'] as $key=>$value){
					if($value){
						$where_d['mid']=$_REQUEST['id'];
						$where_d['stage']=$_REQUEST['stage'][$key];
						if($_REQUEST['contract_money']){
							$where_d['bili']=$_REQUEST['bili'][$key]?$_REQUEST['bili'][$key]:0;
						}else{
							$where_d['bili']=NULL;
						}
						$where_d['stage_money']=$_REQUEST['stage_money'][$key];
						$where_d['stage_date']=$_REQUEST['stage_date'][$key];
						$where_d['ticket_money']=$_REQUEST['ticket_money'][$key];
						$where_d['ticket_date']=$_REQUEST['ticket_date'][$key];
						$where_d['press_date']=$_REQUEST['press_date'][$key];
						$where_d['warn_date']=$_REQUEST['warn_date'][$key];
						$db_project_funds_stage->add($where_d);
					}
				}
			}
			$this->success('操作成功',__APP__."/Financial/project_funds/keyword/".$_REQUEST['keyword']."/mode/".$_REQUEST['mode']."/center_id/".$_REQUEST['center_id']."/type/".$_REQUEST['type']."/ptype/".$_REQUEST['ptype']."");die;
		}
		$this->assign('keyword',$_REQUEST['keyword']);
		$this->assign('mode',$_REQUEST['mode']);
		$this->assign('center_id',$_REQUEST['center_id']);
		$this->assign('type',$_REQUEST['type']);
		$this->assign('ptype',$_REQUEST['ptype']);
		$this->display();
	}
	
	//项目款项-->添加开票 designed by:zhangsen 2018-05-23
	function invoice_add(){
		$db_project_funds=M('db_project_funds');
		$funds=$db_project_funds->field('db_project_funds.id,project_num,project_name')->join('left join db_project on db_project_funds.pid=db_project.id')->order('db_project.id desc')->select();
		$this->assign('funds',$funds);
		$this->assign('mode',$_REQUEST['mode']);
		if($_REQUEST['action']=='add'){
			$db_project_funds_ticket=M('db_project_funds_ticket');
			$where['tid']=$_REQUEST['tid'];
			$where['money']=$_REQUEST['money'];
			$where['date']=$_REQUEST['date'];
			$where['invoice']=$_REQUEST['invoice'];
			$where['add_uid']=$this->login_id;
			$where['add_date']=date('Y-m-d H:i:s');
			$db_project_funds_ticket->add($where);
			$this->success('操作成功',__APP__."/Financial/project_funds/mode/".$_REQUEST['mode']);die;
		}
		$this->display();
	}
	
	//项目款项-->编辑开票 designed by:zhangsen 2018-05-23
	function invoice_edit(){
		$this->assign('id',$_REQUEST['id']);
		$db_project_funds=M('db_project_funds');
		$db_project_funds_stage=M('db_project_funds_stage');
		$where_r['db_project_funds.id']=$_REQUEST['mid'];
		$result=$db_project_funds->field('db_project_funds.*,project_num,project_name')->join('left join db_project on db_project_funds.pid=db_project.id')->where($where_r)->find();
		$where_u['db_project_funds_ticket.id']=$_REQUEST['id'];
		$result_u=$db_project_funds_stage->field('db_project_funds_ticket.*,db_project_funds_stage.stage')->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_u)->order('stage asc')->find();
		//应开票金额
		$where_y['mid']=$_REQUEST['mid'];
		$where_y['stage']=array('eq',$result_u['stage']);
		$result_u['money_y']=$db_project_funds_stage->where($where_y)->sum('ticket_money');
		//已开票金额
		$where_q['db_project_funds_stage.mid']=$_REQUEST['mid'];
		$where_q['stage']=array('eq',$result_u['stage']);
		$where_q['db_project_funds_ticket.id']=array('neq',$_REQUEST['id']);
		$result_u['money_k']=$db_project_funds_stage->field('sum(money) as money')->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_q)->sum('money');;
		$result_u['money_k']=$result_u['money_k']?$result_u['money_k']:0;
		$this->assign('result',$result);
		$this->assign('result_u',$result_u);
		//详情
		$where_k['mid']=$_REQUEST['mid'];
		$where_k['db_project_funds_ticket.id']=array('neq',$_REQUEST['id']);
		$result_s=$db_project_funds_stage->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_k)->order('stage desc')->find();
		unset($where_k['db_project_funds_ticket.id']);
		$result_t=$db_project_funds_stage->where($where_k)->order('stage asc')->select();
		foreach($result_t as $key=>$value){
			//应开票金额
			$where_y['mid']=$_REQUEST['mid'];
			$where_y['stage']=array('eq',$value['stage']);
			$result_t[$key]['money_y']=$db_project_funds_stage->where($where_y)->sum('ticket_money');
			//已开票金额
			$where_w['db_project_funds_stage.mid']=$_REQUEST['mid'];
			$where_w['stage']=array('eq',$value['stage']);
			$where_w['db_project_funds_ticket.id']=array('neq',$_REQUEST['id']);
			$result_t[$key]['money_k']=$db_project_funds_stage->field('sum(money) as money')->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_w)->sum('money');;
			$result_t[$key]['money_k']=$result_t[$key]['money_k']?$result_t[$key]['money_k']:0;
			//判断是否可以选择
			if($result_s['stage']){
				if($value['stage']>($result_s['stage']+1)){
					if($key==count($result_t) and $value['stage']==$result_s['stage']){
						
					}else{
						unset($result_t[$key]);
					}
				}
				if($value['stage']<$result_s['stage']){unset($result_t[$key]);}
			}else{
				if($key!=0){
					unset($result_t[$key]);
				}
			}
		}
		$this->assign('keyword',$_REQUEST['keyword']);
		$this->assign('mode',$_REQUEST['mode']);
		$this->assign('center_id',$_REQUEST['center_id']);
		$this->assign('type',$_REQUEST['type']);
		$this->assign('ptype',$_REQUEST['ptype']);
		$detail=$this->multi_array_sort($result_t);
		$this->assign('detail',$detail);
		if($_REQUEST['action']=='edit'){
			$db_project_funds_ticket=M('db_project_funds_ticket');
			$where['id']=$_REQUEST['id'];
			$where['tid']=$_REQUEST['tid'];
			$where['money']=$_REQUEST['money'];
			$where['date']=$_REQUEST['date'];
			$where['invoice']=$_REQUEST['invoice'];
			$db_project_funds_ticket->save($where);
			$this->success('操作成功',__APP__."/Financial/project_funds/keyword/".$_REQUEST['keyword']."/mode/".$_REQUEST['mode']."/center_id/".$_REQUEST['center_id']."/type/".$_REQUEST['type']."/ptype/".$_REQUEST['ptype']."&p=".$_SESSION['page']);die;
		}
		$this->display();
	}
	
	//项目款项-->添加收款 designed by:zhangsen 2017-09-14
	function receivables_add(){
		$db_project_funds=M('db_project_funds');
		$funds=$db_project_funds->field('db_project_funds.id,pid')->join('inner join db_project_funds_stage on db_project_funds.id=db_project_funds_stage.mid')->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->group('db_project_funds.id')->select();
		foreach($funds as $key=>$value){
			$project=$this->get_project($value['pid']);
			$funds[$key]['project_num']=$project['project_num'];
			$funds[$key]['project_name']=$project['project_name'];
		}
		$this->assign('mode',$_REQUEST['mode']);
		$this->assign('funds',$funds);
		if($_REQUEST['action']=='add'){
			$db_project_funds_money=M('db_project_funds_money');
			$where['sid']=$_REQUEST['tid'];
			$where['money']=$_REQUEST['money'];
			$where['date']=$_REQUEST['date'];
			$where['add_uid']=$this->login_id;
			$where['add_date']=date('Y-m-d H:i:s');
			$db_project_funds_money->add($where);
			$this->success('操作成功',__APP__."/Financial/project_funds/mode/".$_REQUEST['mode']);die;
		}
		$this->display();
	}
	
	//项目款项-->编辑收款 designed by:zhangsen 2017-09-14
	function receivables_edit(){
		$this->assign('id',$_REQUEST['id']);
		$db_project_funds=M('db_project_funds');
		$db_project_funds_stage=M('db_project_funds_stage');
		$db_project_funds_money=M('db_project_funds_money');
		$where_r['db_project_funds.id']=$_REQUEST['mid'];
		$result=$db_project_funds->field('db_project_funds.*,project_num,project_name')->join('left join db_project on db_project_funds.pid=db_project.id')->where($where_r)->find();
		$where_u['db_project_funds_money.id']=$_REQUEST['id'];
		$result_u=$db_project_funds_money->field('db_project_funds_money.*,invoice,db_project_funds_ticket.money as money_y,db_project_funds_ticket.id as tid')->join('inner join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->where($where_u)->order('db_project_funds_money.id asc')->find();
		//开票金额
		$result_u['money_y']=$result_u['money_y'];
		//已收款金额
		$where_q['sid']=$_REQUEST['fid'];
		$where_q['id']=array('neq',$_REQUEST['id']);
		$result_u['money_k']=$db_project_funds_money->where($where_q)->sum('money');
		$result_u['money_k']=$result_u['money_k']?$result_u['money_k']:0;
		$this->assign('result',$result);
		$this->assign('result_u',$result_u);
		//可以选择的发票编号
		$db_project_funds_ticket=M('db_project_funds_ticket');
		$where_k['mid']=$_REQUEST['mid'];
		$where_k['db_project_funds_money.id']=array('neq',$_REQUEST['id']);
		$result_s=$db_project_funds_money->field('db_project_funds_ticket.id')->join('inner join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('inner join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->where($where_k)->order('db_project_funds_ticket.id desc')->find();
		unset($where_k['db_project_funds_money.id']);
		$result_t=$db_project_funds_ticket->field('db_project_funds_ticket.*,db_project_funds_stage.stage')->join('inner join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->where($where_k)->order('db_project_funds_ticket.id asc')->select();
		foreach($result_t as $key=>$value){
			//开票金额
			$result_t[$key]['money_y']=$value['money']-0;
			//已收款金额
			$where_w['sid']=$value['id'];
			$where_w['id']=array('neq',$_REQUEST['id']);
			$result_t[$key]['money_k']=$db_project_funds_money->where($where_w)->sum('money');
			$result_t[$key]['money_k']=$result_t[$key]['money_k']?$result_t[$key]['money_k']-0:0;
		}
		foreach($result_t as $key=>$value){
			
			if($result_s){
				if($value['id']>($result_s['id'])){
					if($key==count($result_t) and $value['id']==$result_s['id']){
						
					}else{
						unset($result_t[$key+1]);
					}
				}
				if($value['id']<$result_s['id']){unset($result_t[$key]);}
			}else{
				if($key!=0){
					unset($result_t[$key]);
				}
			}
		}
		$this->assign('keyword',$_REQUEST['keyword']);
		$this->assign('mode',$_REQUEST['mode']);
		$this->assign('center_id',$_REQUEST['center_id']);
		$this->assign('type',$_REQUEST['type']);
		$this->assign('ptype',$_REQUEST['ptype']);
		$detail=$this->multi_array_sort($result_t);
		$this->assign('detail',$detail);
		if($_REQUEST['action']=='edit'){
			$where['id']=$_REQUEST['id'];
			$where['sid']=$_REQUEST['tid'];
			$where['money']=$_REQUEST['money'];
			$where['date']=$_REQUEST['date'];
			$db_project_funds_money->save($where);
			$this->success('操作成功',__APP__."/Financial/project_funds/keyword/".$_REQUEST['keyword']."/mode/".$_REQUEST['mode']."/center_id/".$_REQUEST['center_id']."/type/".$_REQUEST['type']."/ptype/".$_REQUEST['ptype']."&p=".$_SESSION['page']);die;
		}
		$this->display();
	}
	
	//项目款项-->操作ajax designed by:zhangsen 2018-05-23
	function project_funds_ajax(){
		if($_REQUEST['action']=='project'){
			//排除已经添加的
			$db_project_funds=M('db_project_funds');
			$result_funds=$db_project_funds->field('pid')->group('pid')->select();
			if($result_funds){
				foreach($result_funds as $key=>$value){
					$pids.=$value['pid'].',';
				}
				$where['db_project.id']=array('not in',rtrim($pids,','));
			}
			$db_project=M('db_project');
			$where['center_id']=$_REQUEST['center_id'];
			$where['db_project_bg.status']=1;
			$data=$db_project->field('db_project.*,db_project_bg.status')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->where($where)->order('db_project_bg.pid desc')->select();
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='funds'){
			$db_project_funds=M('db_project_funds');
			$where_f['id']=$_REQUEST['fid'];
			$data['detail']=$db_project_funds->where($where_f)->find();
			$db_project_funds_stage=M('db_project_funds_stage');
			$where_k['mid']=$_REQUEST['fid'];
			$result_s=$db_project_funds_stage->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_k)->order('stage desc')->find();
			$result_t=$db_project_funds_stage->where($where_k)->order('stage asc')->select();
			foreach($result_t as $key=>$value){
				//应开票金额
				$where_y['mid']=$_REQUEST['fid'];
				$where_y['stage']=array('eq',$value['stage']);
				$money_y=$db_project_funds_stage->where($where_y)->sum('ticket_money');
				$result_t[$key]['money_y']=$money_y-0;
				//已开票金额
				$where_w['db_project_funds_stage.mid']=$_REQUEST['fid'];
				$where_w['stage']=array('eq',$value['stage']);
				$result_t[$key]['money_k']=$db_project_funds_stage->field('sum(money) as money')->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_w)->sum('money');;
				$result_t[$key]['money_k']=$result_t[$key]['money_k']?$result_t[$key]['money_k']-0:0;
				//判断是否可以选择
				if($result_s['stage']){
					if($value['stage']>($result_s['stage']+1)){
						if($key==count($result_t) and $value['stage']==$result_s['stage']){
							
						}else{
							unset($result_t[$key]);
						}
					}
					if($value['stage']<$result_s['stage']){unset($result_t[$key]);}
				}else{
					if($key!=0){
						unset($result_t[$key]);
					}
				}
			}
			$data['info']=$this->multi_array_sort($result_t);
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='receivables'){
			$db_project_funds=M('db_project_funds');
			$where_f['id']=$_REQUEST['fid'];
			$data['detail']=$db_project_funds->where($where_f)->find();
			//可以选择的发票编号
			$db_project_funds_ticket=M('db_project_funds_ticket');
			$db_project_funds_money=M('db_project_funds_money');
			$where_k['mid']=$_REQUEST['fid'];
			$result_s=$db_project_funds_money->field('db_project_funds_ticket.id')->join('inner join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('inner join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->where($where_k)->order('db_project_funds_ticket.id desc')->find();
			$result_t=$db_project_funds_ticket->field('db_project_funds_ticket.*,db_project_funds_stage.stage')->join('inner join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->where($where_k)->order('db_project_funds_ticket.id asc')->select();
			foreach($result_t as $key=>$value){	
				//开票金额
				$result_t[$key]['money_y']=$value['money']-0;
				//已收款金额
				$where_w['sid']=$value['id'];
				$result_t[$key]['money_k']=$db_project_funds_money->where($where_w)->sum('money');
				$result_t[$key]['money_k']=$result_t[$key]['money_k']?$result_t[$key]['money_k']-0:0;
			}
			foreach($result_t as $key=>$value){
				
				if($result_s){
					if($value['id']>($result_s['id'])){
						if($key==count($result_t) and $value['id']==$result_s['id']){
							
						}else{
							unset($result_t[$key+1]);
						}
					}
					if($value['id']<$result_s['id']){unset($result_t[$key]);}
				}else{
					if($key!=0){
						unset($result_t[$key]);
					}
				}
			}
			$data['info']=$this->multi_array_sort($result_t);
			echo json_encode($data);die;
		}
	}
	
	//财务费用总览 designed by:zhangsen 2018-06-07
	function cost_list(){
		parent::check_permission('FZ');
		if(!empty($_REQUEST['keyword'])){$keyword=$_REQUEST['keyword'];}
		$this->assign('keyword',$keyword);
		if(!empty($_REQUEST['company'])){$company=$_REQUEST['company'];}else{$company=-10;}
		$this->assign('company',$company);
		if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
		$this->assign('center_id',$center_id);
		if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
		$this->assign('type',$type);
		if(!empty($_REQUEST['c_type'])){$c_type=$_REQUEST['c_type'];}else{$c_type=-10;}
		$this->assign('c_type',$c_type);
		if(!empty($_REQUEST['f_type'])){$f_type=$_REQUEST['f_type'];}else{$f_type=-10;}
		$this->assign('f_type',$f_type);
		if(!empty($_REQUEST['start'])){
			$start=$_REQUEST['start'];
			$lend=date("Y-m-d", strtotime(-date("d",strtotime($start)).'day',strtotime($start)));
			$lstart=date('Y-m-01',strtotime($lend));
			$nstart=date('Y-m-d',strtotime(date('Y-m-t',strtotime($start)).'+1 day'));
			$nend=date('Y-m-d',strtotime(date('Y-m-01',strtotime($nstart)).'+1 month -1 day'));
		}else{
			$start=date('Y-m-01');
			$lstart=date('Y-m-01',strtotime('last month'));
			$lend=date('Y-m-d',strtotime(date('Y-m-01').'-1 day'));
			$nstart=date('Y-m-01',strtotime('next month'));
			$nend=date('Y-m-d',strtotime(date('Y-m-01',strtotime('next month')).'+1 month -1 day'));
		}
		$this->assign('start',$start);
		if(!empty($_REQUEST['end'])){
			$end=$_REQUEST['end'];
		}else{
			$end=date('Y-m-t');
		}
		$this->assign('end',$end);
		//上一周期，下一周期
		$this->assign('last_url',__APP__."/Financial/cost_list/keyword/".$keyword."/center_id/".$center_id."/type/".$type."/c_type/".$c_type."/f_type/".$f_type."/start/".$lstart."/end/".$lend);
		$this->assign('next_url',__APP__."/Financial/cost_list/keyword/".$keyword."/center_id/".$center_id."/type/".$type."/c_type/".$c_type."/f_type/".$f_type."/start/".$nstart."/end/".$nend);
		$where['apply_date']=array('between',array($start,$end));
		$db_ekp_cost=M('db_ekp_cost');
		$db_ekp_cost_detail=M('db_ekp_cost_detail');
		//付款类型下拉
		$cost_list=$this->get_db_universal_type(22);
		$this->assign('cost_list',$cost_list);
		//费用类型下拉
		$fcost_list=$this->get_db_universal_type(23,NULL,NULL,NULL,NULL,$type);
		$this->assign('fcost_list',$fcost_list);
		//权限
		if(get_permission('FZ',1)){
			//所属中心筛选
			$this->assign('center_list',$this->get_oa_dep_list());
			if($center_id!=-10){$where['db_user.center_id']=$center_id;}
			//所属公司
			$this->assign('company_list',$this->get_cost_company());
		}elseif(get_permission('FZ',2)){
			$center_list[0]['id']=$this->login_center_id;
			$center_name=$this->get_oa_dep_name($this->login_center_id);
			$center_list[0]['first_name']=$center_name;
			$this->assign('center_list',$center_list);
			//所属公司
			$this->assign('company_list',$this->get_cost_company($this->login_center_id));
			$where['db_user.center_id']=$this->login_center_id;
		}
		if(!empty($keyword))
		{
			$map['order_number']=array('like','%'.$keyword.'%');
			$map['theme']=array('like','%'.$keyword.'%');
			$map['cost_ascription_user']=array('like','%'.$keyword.'%');
			$map['_logic']='or';
			$where['_complex']=$map;
		}
		if($company!=-10){
			$where['_string']='(type=4 and ascription_project="'.$company.'") or (type<>4 and ascription_unit="'.$company.'")';
		}
		if($type!=-10){$where['type']=$type;}
		if($c_type!=-10){$where['mold_type']=$c_type;}
		if($f_type!=-10){
			switch($type){
				case '2':
					$where['mold_type']=$f_type;
				break;
				case '3':
					if($f_type=='其它'){$where['cost_type']=array(array('exp','is NULL'),array('eq','其它'),'or');}else{$where['cost_type']=$f_type;}
				break;
				case '5':
					$where['mold_type']=$f_type;
				break;
			}
		}
		$where['apply_uid']=array('exp','is not NULL');
		$where['cost_ascription_id']=array('exp','is not NULL');
		$db_user=M('db_user');
		$result=$db_ekp_cost->field('db_ekp_cost.*')->join('db_user on db_ekp_cost.cost_ascription_id=db_user.empl_id')->join('left join db_ekp_cost_detail on db_ekp_cost.order_id=db_ekp_cost_detail.mid')->where($where)->order('db_ekp_cost.apply_date desc,db_ekp_cost.id desc')->group('db_ekp_cost.id')->select();
		foreach($result as $key=>$value){
			$user=$this->get_user($value['cost_ascription_id'],1);
			$center=$this->get_oa_dep_info($user['center_id']);
			$result[$key]['center_name']=$center['DEPT_DESCR'];
			if($value['type']==5){$result[$key]['total']=$value['actual_pay'];}
			if($f_type!=-10){
				switch($type){
					case '1':
						switch($f_type){
							case '住宿费':
								$result[$key]['f_type']=$value['other2'];
							break;
							case '交通费':
								$result[$key]['f_type']=$value['other3'];
							break;
							case '餐费':
								$result[$key]['f_type']=$value['other4'];
							break;
							case '津贴':
								$result[$key]['f_type']=$value['other5'];
							break;
							case '其它':
								$result[$key]['f_type']=$value['other6'];
							break;
						}
					break;
					case '2':
						$result[$key]['f_type']=$value['total'];
					break;
					case '3':
						if($f_type=='其它'){$where_s['cost_type']=array(array('exp','is NULL'),array('eq','其它'),'or');}else{$where_s['cost_type']=$f_type;}
						$where_s['mid']=$value['order_id'];
						$result[$key]['f_type']=$db_ekp_cost_detail->where($where_s)->sum('money')+0;
					break;
					case '4':
						$result[$key]['f_type']=$value['total'];
					break;
					case '5':
						$result[$key]['f_type']=$value['actual_pay'];
					break;
				}
				if($result[$key]['f_type']==0){unset($result[$key]);}
			}
		}
		$count=count($result);
		$Page = new \Think\Page($count,15);
		$result=array_slice($result, $Page->firstRow,$Page->listRows);
		$show=$Page->show();
		$this->assign('result',$result);
		$this->assign('page',$show);
		$this->display();
	}
	
	//财务费用申请统计(按类型) designed by:zhangsen 2018-06-07
	function cost_total_type(){
		parent::check_permission('FZ');
		if(!empty($_REQUEST['keyword'])){$keyword=$_REQUEST['keyword'];}
		$this->assign('keyword',$keyword);
		if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
		$this->assign('type',$type);
		if(!empty($_REQUEST['c_type'])){$c_type=$_REQUEST['c_type'];}else{$c_type=-10;}
		$this->assign('c_type',$c_type);
		if(!empty($_REQUEST['stage'])){$stage=$_REQUEST['stage'];}else{$stage=1;}
		$this->assign('stage',$stage);
		if(!empty($_REQUEST['start'])){
			$start=$_REQUEST['start'];
			$lend=date("Y-m-d", strtotime(-date("d",strtotime($start)).'day',strtotime($start)));
			$lstart=date('Y-m-01',strtotime($lend));
			$nstart=date('Y-m-d',strtotime(date('Y-m-t',strtotime($start)).'+1 day'));
			$nend=date('Y-m-d',strtotime(date('Y-m-01',strtotime($nstart)).'+1 month -1 day'));
		}else{
			$start=date('Y-m-01');
			$lstart=date('Y-m-01',strtotime('last month'));
			$lend=date('Y-m-d',strtotime(date('Y-m-01').'-1 day'));
			$nstart=date('Y-m-01',strtotime('next month'));
			$nend=date('Y-m-d',strtotime(date('Y-m-01',strtotime('next month')).'+1 month -1 day'));
		}
		$this->assign('start',$start);
		if(!empty($_REQUEST['end'])){
			$end=$_REQUEST['end'];
		}else{
			$end=date('Y-m-t');
		}
		$this->assign('end',$end);
		//上一周期，下一周期
		$this->assign('last_url',__APP__."/Financial/cost_total_type/keyword/".$keyword."/center_id/".$center_id."/type/".$type."/c_type/".$c_type."/start/".$start."/start/".$lstart."/end/".$lend);
		$this->assign('next_url',__APP__."/Financial/cost_total_type/keyword/".$keyword."/center_id/".$center_id."/type/".$type."/c_type/".$c_type."/start/".$start."/start/".$nstart."/end/".$nend);
		$where['apply_date']=array('between',array($start,$end));
		$db_ekp_cost=M('db_ekp_cost');
		//付款类型下拉
		$cost_list=$this->get_db_universal_type(22);
		$this->assign('cost_list',$cost_list);
		if(get_permission('FZ',1)){
			//权限
			if($stage==1){
				$result=$this->get_oa_dep_list();
			}else{
				$result=$this->get_cost_company();
			}
		}elseif(get_permission('FZ',2)){
			if($stage==1){
				$result[0]['id']=$this->login_center_id;
				$center_name=$this->get_oa_dep_name($this->login_center_id);
				$result[0]['first_name']=$center_name;
				$where['db_user.center_id']=$this->login_center_id;
			}else{
				$result=$this->get_cost_company($this->login_center_id);
			}			
		}
		if(!empty($keyword))
		{
			$where['cost_ascription_user']=array('like','%'.$keyword.'%');
		}
		if($type!=-10){$where['type']=$type;}
		if($c_type!=-10){$where['mold_type']=$c_type;}
		$where['apply_uid']=array('exp','is not NULL');
		$where['cost_ascription_id']=array('exp','is not NULL');
		$db_user=M('db_user');
		if($type==-10){
			$db_ekp_cost=M('db_ekp_cost');//dump($result);die;
			foreach($result as $key=>$value){
				if($stage==1){
					$where['db_user.center_id']=$value['id'];
				}else{
					$where['_string']='(type=4 and ascription_project="'.$value['ascription_unit'].'") or (type<>4 and ascription_unit="'.$value['ascription_unit'].'")';
				}
				$cost_result=$db_ekp_cost->field('type,sum(total) as total,sum(actual_pay) as stotal')->join('db_user on db_ekp_cost.cost_ascription_id=db_user.empl_id')->where($where)->order('db_ekp_cost.apply_date desc,db_ekp_cost.id desc')->group('type')->select();
				$detail=NULL;
				foreach($cost_result as $kk=>$vv){
					$detail[$vv['type']]['type']=$vv['type'];
					if($vv['type']==5){
						$detail[$vv['type']]['total']=$vv['stotal'];
					}else{
						$detail[$vv['type']]['total']=$vv['total']-0;
					}
					$result[$key]['sum']+=$detail[$vv['type']]['total'];
					$result[$key]['detail']=$detail;
				}
				$result[$key]['zsum']+=$result[$key]['sum'];
			}
			foreach($result as $key=>$value){
				$sum1+=$value['detail'][1]['total'];
				$sum2+=$value['detail'][2]['total'];
				$sum3+=$value['detail'][3]['total'];
				$sum4+=$value['detail'][4]['total'];
				$sum5+=$value['detail'][5]['total'];
				$this->assign('sum1',$sum1);
				$this->assign('sum2',$sum2);
				$this->assign('sum3',$sum3);
				$this->assign('sum4',$sum4);
				$this->assign('sum5',$sum5);
				$this->assign('sum6',$sum1+$sum2+$sum3+$sum4+$sum5);
			}
		}elseif($type==1){
			//费用类型表头
			$fcost_list=$this->get_db_universal_type(23,NULL,NULL,NULL,NULL,$type);
			$this->assign('fcost_list',$fcost_list);
			$this->assign('cocsum',(count($fcost_list)+2));
			$db_ekp_cost=M('db_ekp_cost');
			foreach($result as $key=>$value){
				if($stage==1){
					$where['db_user.center_id']=$value['id'];
				}else{
					$where['_string']='(type=4 and ascription_project="'.$value['ascription_unit'].'") or (type<>4 and ascription_unit="'.$value['ascription_unit'].'")';
				}
				$cost_result=$db_ekp_cost->field('type,other2,other3,other4,other5,other6,total,actual_pay')->join('db_user on db_ekp_cost.cost_ascription_id=db_user.empl_id')->where($where)->order('db_ekp_cost.apply_date desc,db_ekp_cost.id desc')->select();
				foreach($cost_result as $kk=>$vv){
					$result[$key]['other2']+=round($vv['other2'],2);
					$result[$key]['other3']+=round($vv['other3'],2);
					$result[$key]['other4']+=round($vv['other4'],2);
					$result[$key]['other5']+=round($vv['other5'],2);
					$result[$key]['other6']+=round($vv['other6'],2);
					$result[$key]['sum']+=round($vv['total'],2);
				}
			}
			foreach($result as $key=>$value){
				$sum1+=round($value['other2'],2);
				$sum2+=round($value['other3'],2);
				$sum3+=round($value['other4'],2);
				$sum4+=round($value['other5'],2);
				$sum5+=round($value['other6'],2);
				$this->assign('sum1',$sum1);
				$this->assign('sum2',$sum2);
				$this->assign('sum3',$sum3);
				$this->assign('sum4',$sum4);
				$this->assign('sum5',$sum5);
				$this->assign('sum6',round($sum1+$sum2+$sum3+$sum4+$sum5,2));
			}
		}elseif($type!=3){
			//费用类型表头
			$fcost_list=$this->get_db_universal_type(23,NULL,NULL,NULL,NULL,$type);
			$this->assign('cocsum',(count($fcost_list)+2));
			foreach($result as $key=>$value){
				$result[$key]['fcost_list']=$fcost_list;
				foreach($result[$key]['fcost_list'] as $ka=>$va){
					if($stage==1){
						$where['db_user.center_id']=$value['id'];
					}else{
						$where['_string']='(type=4 and ascription_project="'.$value['ascription_unit'].'") or (type<>4 and ascription_unit="'.$value['ascription_unit'].'")';
					}
					if($type!=4){$where['mold_type']=$va['name1'];}
					$cost_result=$db_ekp_cost->field('type,sum(total) as total,sum(actual_pay) as stotal')->join('db_user on db_ekp_cost.cost_ascription_id=db_user.empl_id')->where($where)->order('db_ekp_cost.apply_date desc,db_ekp_cost.id desc')->group('type')->find();
					if($type==5){
						$result[$key]['fcost_list'][$ka]['total']=round($cost_result['stotal'],2);
						$result[$key]['sum']+=round($cost_result['stotal'],2);
					}else{
						$result[$key]['fcost_list'][$ka]['total']=round($cost_result['total'],2);
						$result[$key]['sum']+=round($cost_result['total'],2);
					}
				}
				$sum+=round($result[$key]['sum'],2);
			}
			foreach($result as $key=>$value){
				foreach($result[$key]['fcost_list'] as $ka=>$va){
					foreach($fcost_list as $k1=>$v1){
						if($v1['id']==$va['id']){
							$fcost_list[$k1]['sum']+=round($result[$key]['fcost_list'][$ka]['total'],2);
						}
					}
				}
			}
			$this->assign('fcost_list',$fcost_list);
			$this->assign('sum',$sum);
		}else{
			//费用类型表头
			$fcost_list=$this->get_db_universal_type(23,NULL,NULL,NULL,NULL,$type);
			$this->assign('cocsum',(count($fcost_list)+2));
			foreach($result as $key=>$value){
				$result[$key]['fcost_list']=$fcost_list;
				foreach($result[$key]['fcost_list'] as $ka=>$va){
					if($stage==1){
						$where['db_user.center_id']=$value['id'];
					}else{
						$where['_string']='(type=4 and ascription_project="'.$value['ascription_unit'].'") or (type<>4 and ascription_unit="'.$value['ascription_unit'].'")';
					}
					if($va['name1']=='其它'){$where['cost_type']=array(array('exp','is NULL'),array('eq',$va['name1']),'or');}else{$where['cost_type']=$va['name1'];}
					$cost_result=$db_ekp_cost->join('db_user on db_ekp_cost.cost_ascription_id=db_user.empl_id')->join('db_ekp_cost_detail on db_ekp_cost.order_id=db_ekp_cost_detail.mid')->where($where)->order('db_ekp_cost.apply_date desc,db_ekp_cost.id desc')->sum('money');
					$result[$key]['fcost_list'][$ka]['total']=round($cost_result,2);
					$result[$key]['sum']+=round($cost_result,2);
				}
				$sum+=round($result[$key]['sum'],2);
			}
			foreach($result as $key=>$value){
				foreach($result[$key]['fcost_list'] as $ka=>$va){
					foreach($fcost_list as $k1=>$v1){
						if($v1['id']==$va['id']){
							$fcost_list[$k1]['sum']+=round($result[$key]['fcost_list'][$ka]['total'],2);
						}
					}
				}
			}
			$this->assign('fcost_list',$fcost_list);
			$this->assign('sum',$sum);
		}
		$this->assign('result',$result);
		$this->assign('page',$show);
		//所属中心筛选
		$this->assign('center_list',$this->get_oa_dep_list());
		$this->display();
	}

    //财务中心-->财务费用总览统计(按周期) designed by:wuhuili 2018-06-14
    function cost_list_statistics(){
		if(!empty($_REQUEST['keyword'])){$keyword=$_REQUEST['keyword'];}
		$this->assign('keyword',$keyword);
        if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
        $this->assign('type',$type);
        if(!empty($_REQUEST['c_type'])){$c_type=$_REQUEST['c_type'];}else{$c_type=-10;}
        $this->assign('c_type',$c_type);
        if(!empty($_REQUEST['f_type'])){$f_type=$_REQUEST['f_type'];}else{$f_type=-10;}
        $this->assign('f_type',$f_type);
		if(!empty($_REQUEST['stage'])){$stage=$_REQUEST['stage'];}else{$stage=1;}
		$this->assign('stage',$stage);
        $db_ekp_cost=M('db_ekp_cost');
        //付款类型下拉
        $cost_list=$this->get_db_universal_type(22);
        $this->assign('cost_list',$cost_list);
        //费用类型下拉
        $fcost_list=$this->get_db_universal_type(23,NULL,NULL,NULL,NULL,$type);
        $this->assign('fcost_list',$fcost_list);
        if(get_permission('FZ',1)){
			//权限
			if($stage==1){
				$center_list=$this->get_oa_dep_list();
			}else{
				$center_list=$this->get_cost_company();
			}
		}elseif(get_permission('FZ',2)){
			if($stage==1){
				$center_list[0]['id']=$this->login_center_id;
				$center_name=$this->get_oa_dep_name($this->login_center_id);
				$center_list[0]['first_name']=$center_name;
				$where['db_user.center_id']=$this->login_center_id;
			}else{
				$center_list=$this->get_cost_company($this->login_center_id);
			}			
		}
		if(!empty($keyword))
		{
			$where['cost_ascription_user']=array('like','%'.$keyword.'%');
		}
        $years=$db_ekp_cost->field('MIN(apply_date) AS min_date, MAX(apply_date) AS max_date')->find();
        $start_year=substr($years['min_date'],0,4);
        $end_year=substr($years['max_date'],0,4);
        for($i=$start_year;$i<=$end_year;$i++){
            $year_list[]=$i;
        }
        $this->assign('year_list',$year_list);
        $year=empty($_REQUEST['year'])?date('Y'):$_REQUEST['year'];
        $this->assign('year',$year);
        if($type!=-10){$where['type']=$type;}
        if($c_type!=-10){$where['mold_type']=$c_type;}
        $str='';
        if($f_type!=-10){
            switch($type){
                case '2':
                    $where['mold_type']=$f_type;
                    break;
                case '5':
                    $where['mold_type']=$f_type;
                    break;
            }
        }
		$where['apply_uid']=array('exp','is not NULL');
		$where['cost_ascription_id']=array('exp','is not NULL');
        $field='cost_ascription_id';
        if($type==3){
            if($f_type!=-10){
                if($f_type=='其它') {
                    $str=" and (db_ekp_cost_detail.cost_type='".$f_type."' or db_ekp_cost_detail.cost_type is NULL)";
                }else{
                    $where['db_ekp_cost_detail.cost_type']=$f_type;
                }
            }
            $field.=',sum(db_ekp_cost_detail.money) as sum_pay';
            $db_user=M('db_user');
            foreach($center_list as $key=>$value){
                $sum_total=0;
                for($j=1;$j<13;$j++){
                    $start_date=date($year.'-'.$j.'-01');
                    $end_time=strtotime($year.'-'.$j.'-01');
                    $end_date=date("Y-m-t",$end_time);
                    if($stage==1){
						$where_b['db_user.center_id']=$value['id'];
					}else{
						$where_b['_string']='(type=4 and ascription_project="'.$value['ascription_unit'].'") or (type<>4 and ascription_unit="'.$value['ascription_unit'].'")';
					}
                    $where_b['db_ekp_cost.apply_date']=array('between',array($start_date,$end_date));
                    $where_b=array_merge($where_b,(array)$where);
                    $sum_pay=$db_ekp_cost->field($field)->join('db_ekp_cost_detail on db_ekp_cost.order_id=db_ekp_cost_detail.mid')->join('db_user on db_ekp_cost.cost_ascription_id=db_user.empl_id')->where($where_b)->find();
                    $sum_total=$sum_total+$sum_pay['sum_pay'];
                    $center_list[$key]['sum_total'][]=$sum_pay['sum_pay'];
                }
                $center_list[$key]['sum_total'][]=$sum_total+0;
            }
        }else{
            if($type==1){
                if($f_type!=-10){
                    switch($f_type){
                        case '住宿费':
                            $field.=',sum(db_ekp_cost.other2) as sum_pay';
                            break;
                        case '交通费':
                            $field.=',sum(db_ekp_cost.other3) as sum_pay';
                            break;
                        case '餐费':
                            $field.=',sum(db_ekp_cost.other4) as sum_pay';
                            break;
                        case '津贴':
                            $field.=',sum(db_ekp_cost.other5) as sum_pay';
                            break;
                        case '其它':
                            $field.=',sum(db_ekp_cost.other6) as sum_pay';
                            break;
                    }
                }else{
                    $field.=',sum(db_ekp_cost.total) as sum_pay';
                }
            }else{
                $field.=',sum(db_ekp_cost.total) as sum_pay';
            }
            $db_user=M('db_user');
            foreach($center_list as $key=>$value){
                $sum_total=0;
                for($j=1;$j<13;$j++){
                    $start_date=date($year.'-'.$j.'-01');
                    $end_time=strtotime($year.'-'.$j.'-01');
                    $end_date=date("Y-m-t",$end_time);
                    if($stage==1){
						$where_b['db_user.center_id']=$value['id'];
					}else{
						$where_b['_string']='(type=4 and ascription_project="'.$value['ascription_unit'].'") or (type<>4 and ascription_unit="'.$value['ascription_unit'].'")';
					}
                    $where_b['db_ekp_cost.apply_date']=array('between',array($start_date,$end_date));
                    $where_b=array_merge($where_b,(array)$where);
                    $sum_pay=$db_ekp_cost->field($field)->join('db_user on db_ekp_cost.cost_ascription_id=db_user.empl_id')->where($where_b)->find();
                    $sum_total=$sum_total+$sum_pay['sum_pay'];
                    $center_list[$key]['sum_total'][]=$sum_pay['sum_pay'];
                }
                $center_list[$key]['sum_total'][]=$sum_total+0;
            }
        }
        $total=array();
        foreach($center_list as $k=>$v){
            $total[0]['total']+=$v['sum_total'][0];
            $total[1]['total']+=$v['sum_total'][1];
            $total[2]['total']+=$v['sum_total'][2];
            $total[3]['total']+=$v['sum_total'][3];
            $total[4]['total']+=$v['sum_total'][4];
            $total[5]['total']+=$v['sum_total'][5];
            $total[6]['total']+=$v['sum_total'][6];
            $total[7]['total']+=$v['sum_total'][7];
            $total[8]['total']+=$v['sum_total'][8];
            $total[9]['total']+=$v['sum_total'][9];
            $total[10]['total']+=$v['sum_total'][10];
            $total[11]['total']+=$v['sum_total'][11];
            $total[12]['total']+=$v['sum_total'][12];
        }
        $this->assign('total',$total);
        $this->assign('center_list',$center_list);
        $this->display();
    }
	
	//差旅费用报销申请 designed by:zhangsen 2018-06-07
	function cost_travel_detail(){
		$db_ekp_cost=M('db_ekp_cost');
		$db_ekp_cost_detail=M('db_ekp_cost_detail');
		$where['id']=$_REQUEST['id'];
		$result=$db_ekp_cost->where($where)->find();
		$user=$this->get_user($result['cost_ascription_id'],1);
		$center=$this->get_oa_dep_info($user['center_id']);
		$result['center_name']=$center['DEPT_DESCR'];
		$where_d['mid']=$result['order_id'];
		$detail=$db_ekp_cost_detail->where($where_d)->select();
		$this->assign('detail',$detail);
		$this->assign('result',$result);
		$this->display();
	}
	
	//费用报销单详情 designed by:zhangsen 2018-06-07
	function cost_apply_detail(){
		$db_ekp_cost=M('db_ekp_cost');
		$db_ekp_cost_detail=M('db_ekp_cost_detail');
		$where['id']=$_REQUEST['id'];
		$result=$db_ekp_cost->where($where)->find();
		$user=$this->get_user($result['cost_ascription_id'],1);
		$center=$this->get_oa_dep_info($user['center_id']);
		$result['center_name']=$center['DEPT_DESCR'];
		$where_d['mid']=$result['order_id'];
		$detail=$db_ekp_cost_detail->where($where_d)->select();
		$this->assign('detail',$detail);
		$this->assign('result',$result);
		$this->display();
	}
	
	//付款申请详情 designed by:zhangsen 2018-06-07
	function cost_payment_detail(){
		$db_ekp_cost=M('db_ekp_cost');
		$db_ekp_cost_detail=M('db_ekp_cost_detail');
		$where['id']=$_REQUEST['id'];
		$result=$db_ekp_cost->where($where)->find();
		$user=$this->get_user($result['cost_ascription_id'],1);
		$center=$this->get_oa_dep_info($user['center_id']);
		$result['center_name']=$center['DEPT_DESCR'];
		$where_d['mid']=$result['order_id'];
		$detail=$db_ekp_cost_detail->where($where_d)->select();
		$this->assign('detail',$detail);
		$this->assign('result',$result);
		$this->display();
	}
	
	//营销专项费用报销申请详情 designed by:zhangsen 2018-06-07
	function cost_marketing_apply_detail(){
		$db_ekp_cost=M('db_ekp_cost');
		$db_ekp_cost_detail=M('db_ekp_cost_detail');
		$where['id']=$_REQUEST['id'];
		$result=$db_ekp_cost->where($where)->find();
		$user=$this->get_user($result['cost_ascription_id'],1);
		$center=$this->get_oa_dep_info($user['center_id']);
		$result['center_name']=$center['DEPT_DESCR'];
		$where_d['mid']=$result['order_id'];
		$detail=$db_ekp_cost_detail->where($where_d)->select();
		$this->assign('detail',$detail);
		$this->assign('result',$result);
		$this->display();
	}
	
	//营销专项付款申请详情 designed by:zhangsen 2018-06-07
	function cost_marketing_payment_detail(){
		$db_ekp_cost=M('db_ekp_cost');
		$where['id']=$_REQUEST['id'];
		$result=$db_ekp_cost->where($where)->find();
		$user=$this->get_user($result['cost_ascription_id'],1);
		$center=$this->get_oa_dep_info($user['center_id']);
		$result['center_name']=$center['DEPT_DESCR'];
		$this->assign('result',$result);
		$this->display();
	}
	
	//财务费用申请-->获取公司数据 designed by:zhangsen 2018-06-19
	function get_cost_company($center_id=NULL){
		if($center_id){
			$db_user=M('db_user');
			$where['center_id']=$center_id;
			$where['empl_id']=array('exp','is not NULL');
			$user=$db_user->where($where)->select();
			if($user){
				foreach($user as $key=>$value){
					$uids.=$value['empl_id'].',';
				}
				$uids='and cost_ascription_id in('.rtrim($uids,',').')';
			}
		}
		$str="(SELECT ascription_project as ascription_unit from db_ekp_cost where type=4 $uids GROUP BY ascription_project)   UNION  (SELECT ascription_unit from db_ekp_cost where type<>4 $uids GROUP BY ascription_unit) ORDER BY ascription_unit";
		$Model=M();
		$result=$Model->query($str);
		return $result;
	}
	
    //项目款项-->预警、催款提醒(定时任务) designed by:wuhuili 2018-06-06
    function project_funds_alert(){
        $db_project_funds_stage=M('db_project_funds_stage');
        $map['press_date']=date('Y-m-d');
        $map['warn_date']=date('Y-m-d');
        $map['_logic']='or';
        $where_stage['_complex']=$map;
        $stages=$db_project_funds_stage->where($where_stage)->field('db_project_funds_stage.*,db_project.id as project_id,db_project.project_name,db_project.project_num')->join('left join db_project_funds on db_project_funds.id=db_project_funds_stage.mid')->join('left join db_project on db_project.id=db_project_funds.pid')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->select();
        //人员
        $where['_string']="find_in_set('35',permissions_id)";
        $where['status']=1;
        $user_list=M('db_user')->field('id,username,permissions_id')->where($where)->select();
        $date=date('Y-m-d');
        foreach($stages as $key=>$value){
            foreach($user_list as $user){
                $to_users_array=array();
                $to_user['id']=$user['id'];
                $to_user['username']=$user['username'];
                $to_users_array[]=$to_user;
                if($value['press_date']==$date){
                    $this->alert_send('项目款项催款',$to_users_array,'您的项目【'.$value['project_num'].'/'.$value['project_name'].'】【第'.$value['stage'].'阶段】项目款项催款日期到了，请及时处理。',-1,NULL,'系统消息',1);
                }
                if($value['warn_date']==$date){
                    $this->alert_send('项目款项预警',$to_users_array,'您的项目【'.$value['project_num'].'/'.$value['project_name'].'】【第'.$value['stage'].'阶段】项目款项预警日期到了，请注意查看。',-1,NULL,'系统消息',1);
                }
            }

        }
    }
}
?>